//
//  CheXiaoViewController.m
//  ShouKuang
//
//  Created by CodeYY on 2019/6/3.
//  Copyright © 2019 CodeYY. All rights reserved.
//

#import "CheXiaoViewController.h"
#import "DuanXinYanZhengViewController.h"
@interface CheXiaoViewController ()
@property(nonatomic,weak)UIButton *codeBtn;
@property(nonatomic, strong)dispatch_source_t timer;
@end

@implementation CheXiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavBar];
    [self initCustomUI];
    
    
    // Do any additional setup after loading the view.
}

-(void)initNavBar
{
    WEAKSELF
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"nav_back_red"] forState:UIControlStateNormal];
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn setTitleColor:YYRGB(129, 129, 129) forState:UIControlStateNormal];
    [leftBtn jk_setImagePosition:LXMImagePositionLeft spacing:5];
    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.title = @"撤销";
}



-(void)initCustomUI
{
    __weak typeof(self) weakSelf = self;
    UIView *contentView = [UIView new];
    [self.view addSubview:contentView];
    contentView.backgroundColor = [UIColor whiteColor];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(70);
        make.height.mas_equalTo(160);
    }];
    
    UILabel *tishiLab = [UILabel new];
    [contentView addSubview:tishiLab];
    [tishiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(60);
        make.top.offset(40);
    }];
    tishiLab.text = @"输入验证码";
    
    UITextField *textField = [UITextField new];
    [contentView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tishiLab);
        make.top.equalTo(tishiLab.mas_bottom).offset(30);
        
    }];
    UIView *line = [UIView new];
    [textField addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(0.5);
    }];
    line.backgroundColor = [UIColor lightGrayColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.left.equalTo(textField.mas_right).offset(5);
        make.bottom.equalTo(textField);
        make.right.offset(-60);
    }];
    btn.backgroundColor = [UIColor darkTextColor];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    [btn setTitle:@"验证码" forState:UIControlStateNormal];
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        dispatch_resume(weakSelf.timer);
    }];
    _codeBtn = btn;
    
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(contentView.mas_bottom).offset(40);
        make.height.mas_equalTo(50);
    }];
    sureBtn.backgroundColor = [UIColor whiteColor];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        DuanXinYanZhengViewController *vc = [DuanXinYanZhengViewController new];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

-(dispatch_source_t)timer
{
    __weak typeof(self) weakSelf = self;
    if (!_timer) {
        __block int timeout=30; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        _timer = timer;
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(timer, ^{
            
            if (timeout <= 0) { // 倒计时结束
                dispatch_source_cancel(timer);
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.codeBtn.enabled = NO;
                    [self.codeBtn setTitle:[NSString stringWithFormat:@"%d秒",timeout] forState:UIControlStateNormal];
                });
                timeout--;
            }
        });
        
        dispatch_source_set_cancel_handler(timer, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.codeBtn setTitle:@"验证码" forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = YES;
            });
            weakSelf.timer = nil;
        });
     
    }
    return _timer;
}



@end
